package com.ynkbny.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.Role;
import com.ynkbny.domain.pojo.RoleWithRouter;
import com.ynkbny.mapper.RoleMapper;
import com.ynkbny.mapper.RoleWithRouterMapper;
import com.ynkbny.service.RoleService;
import com.ynkbny.util.SearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zelin.wang
 * @description 角色信息实现类
 * @date 8/16/2023
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    RoleWithRouterMapper roleWithRouterMapper;

    /**
     * @param routerId id
     * @param role 角色信息
     * @return 结果
     * @author : zelinwang
     */
    @Override
    public Boolean editWithRouter(Role role, List<Integer> routerId) {
        log.info("入参：{},{}",role,routerId);
        RoleWithRouter roleWithRouter = new RoleWithRouter();
        try {
            for (Integer item : routerId) {
                roleWithRouter.setRoleId(role.getId());
                roleWithRouter.setRouterId(item);
                roleWithRouterMapper.insert(roleWithRouter);
            }
            log.info("方法返回值：true");
            return true;
        } catch (Exception e) {
            log.info("方法返回值：false");
            return false;
        }


    }

    @Override
    public PageResult<Role> pageList(String conditionJson,int pageNum, int pageSize) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> queryWrapper = SearchUtil.parseWhereSql(null, null, conditionJson);
        return new PageResult<>(this.baseMapper.selectPage(page,queryWrapper));
    }
}
